﻿#ifndef CLIENTCALLBACK_H
#define CLIENTCALLBACK_H
#include <vector>
#include <librtc.h>
#include <QMap>
#include <QVariant>
#include <QWidget>
#include "globalinstance.h"

class kkim_login_param
{
public:
    qint64 uid;
    QString account;
    QByteArray hashed_password;
    kkim_login_param(): uid(0){}
};

class QAudioInput;

class ClientCallback : public QObject, public GlobalInstance<ClientCallback>,public librtc_cb
{
    Q_OBJECT
    friend class GlobalInstance<ClientCallback>;

    explicit ClientCallback();
public:
    static const QSize cameraSize;
    xos::int32 ntfy_user_evnt(xos::uint8* buf, xos::uint32 len) Q_DECL_OVERRIDE;
    xos::int32 ntfy_room_evnt(xos::uint8* buf, xos::uint32 len) Q_DECL_OVERRIDE;

    xos::int32 ntfy_local_audio(xos::int64 rid, xos::int64 uid, xos::uint8* audio,  xos::uint32  audio_len, const xos::int32  sample_rate, const xos::int32  channels) Q_DECL_OVERRIDE;
    xos::int32 ntfy_remote_audio(xos::int64 rid, xos::int64 uid, xos::uint8* audio, xos::uint32  audio_len, const xos::int32  sample_rate, const xos::int32  channels) Q_DECL_OVERRIDE;
    xos::int32 ntfy_local_video(xos::int64 rid, xos::int64 uid, void* data, xos::int32  data_len, const xos::int32  width, const xos::int32  height, const xos::int32  pic_type) Q_DECL_OVERRIDE;
    xos::int32 ntfy_remote_video(xos::int64 rid, xos::int64 uid, void* data, xos::int32  data_len, const xos::int32  width, const xos::int32  height, const xos::int32  pic_type) Q_DECL_OVERRIDE;

    bool im_login();
    bool im_login(QString account, const QByteArray& hashedPW);
    void im_logout();
    QByteArray im_send_msg(qint64 uid, const QString &name, int msg_type, const QString& text, qint64 time_len = 0);
    void im_delete_msg(qint64 uid);
    void im_mark_peer_read(qint64 uid, const QByteArray &mid);
    void im_mark_read(qint64 uid, const QByteArray &mid = QByteArray());

    QByteArray channel_send_msg(qint64 rid, const QString &name, int msg_type, const QString& text, qint64 time_len = 0);
    void channel_send_msg(const QVariantHash &msg);
    void channel_delete_msg(qint64 rid);
    void channel_apply_ptt(qint64 rid, qint64 uid);
    void channel_apply_ptt(qint64 rid);
    void channel_apply_video(qint64 rid, qint64 uid);
    void channel_apply_video(qint64 rid);
    void channel_free_mic(qint64 rid, qint64 uid);
    void channel_free_mic(qint64 rid);
    void channel_grant_mic(qint64 rid, qint64 uid, int mid);
    void channel_get_onlines(qint64 rid);
    void channel_mark_read(qint64 rid, const QByteArray &mid = QByteArray());

    void set_video_list(const QSet<qint64>& rids);
    void set_camera_id(int id);
    void start_camera_test();
    void stop_camera_test();
    void set_speaker_id(int id);
    void set_speaker_mute(qint64 rid, qint64 uid, bool mute);
    void set_speaker_volume(int value);
    void start_speaker_test();
    void stop_speaker_test();
    void set_microphone_id(int id);
    void set_microphone_mute(qint64 rid, bool mute);
    void start_microphone_test();
    void stop_microphone_test();
    void set_room_mute_list(const QByteArrayList &room_list);
    void set_user_mute_list(const QByteArrayList &user_list);
    void set_p2p_user_list(const QByteArrayList &user_list);
    void create_p2p_channel(qint64 uid, bool video = false);
    void free_p2p_channel();
    void set_mds_list();

    const qint64 &get_my_id() const;
    const std::string get_my_id_stdstring() const;
    const QString get_my_id_QString() const;
    const kkim_login_param &get_login_param() const;

    static void set_pxy_servers(const QString &str);
    static void set_protocol_version(const QString &str);
    static QString getErrorString(int code);

    void setAudioInput(int idx);
    void setAudioInputVolume(int volume);

signals:
    void signal_user_evnt(int cmd_type, QVariantHash msg);
    void signal_room_evnt(QVariantHash msg);
    void signal_local_video(qint64 room_id, qint64 user_id, QPixmap pix);
    void signal_remote_video(qint64 room_id, qint64 user_id, QPixmap pix);
    void signal_local_audio(qint64 room_id, qint64 user_id, int volume);
    void signal_remote_audio(qint64 room_id, qint64 user_id, int volume);
    void signal_video_test(QPixmap pix);
    void signal_speaker_test(int volume);
    void signal_phone_test(int volume);
private:
    kkim_login_param im_login_param;
    QWidget popup;
    QByteArray mdsList;
    QAudioInput* audioInput;
};

#endif // CLIENTCALLBACK_H
